# coding=utf-8
from . import base
from .component import Component

## 实现一个组件，需要写如下几个方法：
class EnSelf(Component):

    ## 依赖的组件，返回类型为 string 数组，例如 return ["docker"]，表示该组件依赖的其他组件
    def dependencies(self):
        return []



    ## 检测安装状态，返回true或者false，用于显示是否已经安装该组件
    def check(self):
        return True



    ## 检测运行状态，返回true或者false，表示该组件是否正在运行
    def status(self):
        return True




    ## 安装 组件需要运行的命令行
    def install(self, rVersion : str = None):
        return;


    ## 更新
    def update(self, rVersion : str = None):
        EN_PATH = base.get_env("EN_PATH")
        if base.is_windows():
            base.cmd_in_dir(f"{EN_PATH}", "update.bat")
        else:
            base.cmd_in_dir(f"{EN_PATH}", "./update.sh")


    ## 卸载
    def uninstall(self):
        EN_PATH = base.get_env("EN_PATH")
        en_file = "/etc/profile"
        lines = []
        with open(en_file) as content:
            line = content.split("\n")
            if not line.startswith("export EN_PATH="):
                lines.append(line)
        
        base.writeFile("/etc/profile", "\n".join(content))
        base.delete_dir(EN_PATH)



from . import component_manager as cm
cm.ALL_COMPONENTS['en'] = EnSelf()